#include <applicationTask.h>
#include <ColorMixingTask.h>
#include <lin_device.h>
#include <adc_device.h>
#include <uart_device.h>
#include <gpio_device.h>
#include <linMasterTask.h>
#include <SoftTimerTask.h>

TaskState_t applState = TASK_STATE_INIT;
void App1TimerExpired(SoftTimer_t *timer);
void App2TimerExpired(SoftTimer_t *timer);
void colorCompensationTimerExpired(SoftTimer_t *timer);

#define SECTION_TIME_1    500  //ms
#define SECTION_TIME_2    80  //ms

SoftTimer_t App1Timer = {
    .mode     = TIMER_PERIODIC_MODE,            //循环模式定时
    .interval = SECTION_TIME_1,
    .handler  = App1TimerExpired
};

//SoftTimer_t App2Timer = {
//    .mode     = TIMER_PERIODIC_MODE,            //循环模式定时
//    .interval = SECTION_TIME_2,
//    .handler  = App2TimerExpired
//};

//SoftTimer_t colorCompensationTimer = {
//    .mode     = TIMER_PERIODIC_MODE,
//    .interval = 200,
//    .handler  = colorCompensationTimerExpired
//};

//多彩1颜色组1
RGB_t rgbTable_Flow_1_1[LED_MAX_NUM*TABLE_SIZE_Flow_1] =     //100ms   0.7
{
    {255,255,0,100},    {0,0,0,0},          {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {255,255,0,100},    {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {255,255,0,100},    {255,255,0,100},   {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {255,255,0,100},    {255,255,0,100},   {255,255,0,100},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {255,255,0,100},    {255,255,0,100},   {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {255,255,0,100},    {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {255,255,0,100},    {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {255,255,0,100},    {0,0,0,0},          {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {0,0,0,0},          {0,0,0,0},          {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},      
};

//多彩2颜色组1
RGB_t rgbTable_Flow_2_1[LED_MAX_NUM*TABLE_SIZE_Flow_2] =     //100ms   0.7
{
    {175,255,64,100},    {0,0,0,0},        {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {114,255,3,100},    {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {114,255,3,100},    {6,255,2,100},     {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {114,255,3,100},    {6,255,2,100},     {1,255,125,100},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {114,255,3,100},    {6,255,2,100},     {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {114,255,3,100},    {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {255,198,0,100},     {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {255,3,1,100},    {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {175,255,64,100},    {0,0,0,0},        {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
    {0,0,0,0},           {0,0,0,0},        {0,0,0,0},           {0,0,0,0},          {0,0,0,0},         {0,0,0,0},
};

RGB_t rgbTable_Flow_3[LED_MAX_NUM*5] =     //100ms   0.7
{
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},     {255,255,0,100},   {255,255,0,100},    {255,255,0,100},
    {6,255,2,100},      {6,255,2,100},      {6,255,2,100},       {6,255,2,100},     {6,255,2,100},      {6,255,2,100},
    {1,255,125,100},    {1,255,125,100},    {1,255,125,100},     {1,255,125,100},   {1,255,125,100},    {1,255,125,100},
    {1,20,255,100},     {1,20,255,100},     {1,20,255,100},      {1,20,255,100},    {1,20,255,100},     {1,20,255,100},
    {255,0,255,100},    {255,0,255,100},    {255,0,255,100},     {255,0,255,100},   {255,0,255,100},    {255,0,255,100},
};

//单色
RGB_t rgbTable_SingleColor[LED_MAX_NUM*TABLE_SIZE_SinClr] = 
{
    //冷白
    {175,255,64,100},   {175,255,64,100},  {175,255,64,100},  {175,255,64,100},  {175,255,64,100},  {175,255,64,100},
    //红
    {255,3,1,100},      {255,3,1,100},     {255,3,1,100},     {255,3,1,100},     {255,3,1,100},     {255,3,1,100},
    //琥珀
    {255,198,0,100},    {255,198,0,100},   {255,198,0,100},   {255,198,0,100},   {255,198,0,100},   {255,198,0,100},
    //绿黄
    {114,255,3,100},    {114,255,3,100},   {114,255,3,100},   {114,255,3,100},   {114,255,3,100},   {114,255,3,100},
    //绿色
    {6,255,2,100},      {6,255,2,100},     {6,255,2,100},     {6,255,2,100},     {6,255,2,100},     {6,255,2,100},
    //天蓝
    {1,255,125,100},    {1,255,125,100},   {1,255,125,100},   {1,255,125,100},   {1,255,125,100},   {1,255,125,100},
    //深蓝
    {1,20,255,100},     {1,20,255,100},    {1,20,255,100},    {1,20,255,100},    {1,20,255,100},    {1,20,255,100},
    //紫丁香
    {142,60,255,100},   {142,60,255,100},  {142,60,255,100},  {142,60,255,100},  {142,60,255,100},  {142,60,255,100},
};

//单色呼吸&多色呼吸
RGB_t rgbTable_Breath[LED_MAX_NUM*TABLE_SIZE_Brth] = 
{
    //冷白    0x00
    {175,255,64,99},  {175,255,64,99},  {175,255,64,99},  {175,255,64,99},  {175,255,64,99},  {175,255,64,99},
    {175,255,64,0},   {175,255,64,0},   {175,255,64,0},   {175,255,64,0},   {175,255,64,0},   {175,255,64,0},   
    //红     0x02
    {255,3,1,99},  {255,3,1,99},  {255,3,1,99},  {255,3,1,99},  {255,3,1,99},  {255,3,1,99},
    {255,3,1,0},   {255,3,1,0},   {255,3,1,0},   {255,3,1,0},   {255,3,1,0},   {255,3,1,0},
    //琥珀    0x04
    {255,198,0,99},  {255,198,0,99},  {255,198,0,99},  {255,198,0,99},  {255,198,0,99},  {255,198,0,99},
    {255,198,0,0},   {255,198,0,0},   {255,198,0,0},   {255,198,0,0},   {255,198,0,0},   {255,198,0,0},
    //绿黄    0x06
    {114,255,3,99},  {114,255,3,99},  {114,255,3,99},  {114,255,3,99},  {114,255,3,99},  {114,255,3,99},
    {114,255,3,0},   {114,255,3,0},   {114,255,3,0},   {114,255,3,0},   {114,255,3,0},   {114,255,3,0},
    //绿色    0x08
    {6,255,2,99},  {6,255,2,99},  {6,255,2,99},  {6,255,2,99},  {6,255,2,99},  {6,255,2,99},
    {6,255,2,0},   {6,255,2,0},   {6,255,2,0},   {6,255,2,0},   {6,255,2,0},   {6,255,2,0},
    //天蓝    0x0A
    {1,255,125,99},  {1,255,125,99},  {1,255,125,99},  {1,255,125,99},  {1,255,125,99},  {1,255,125,99},
    {1,255,125,0},   {1,255,125,0},   {1,255,125,0},   {1,255,125,0},   {1,255,125,0},   {1,255,125,0},
    //深蓝    0x0C
    {1,20,255,99},  {1,20,255,99},  {1,20,255,99},  {1,20,255,99},  {1,20,255,99},  {1,20,255,99},
    {1,20,255,0},   {1,20,255,0},   {1,20,255,0},   {1,20,255,0},   {1,20,255,0},   {1,20,255,0},
    //紫丁香   0x0E
    {142,60,255,99},  {142,60,255,99},  {142,60,255,99},  {142,60,255,99},  {142,60,255,99},  {142,60,255,99},
    {142,60,255,0},   {142,60,255,0},   {142,60,255,0},   {142,60,255,0},   {142,60,255,0},   {142,60,255,0},
};
                   
//无极变色
RGB_t rgbTable_TransClr[LED_MAX_NUM*TABLE_SIZE_TransClr] =         //1500ms  0.7
{
    //红
    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},
    //橙
    {255,125,0,100},  {255,125,0,100},  {255,125,0,100},  {255,125,0,100},  {255,125,0,100},  {255,125,0,100},
    //黄
    {255,255,0,100},  {255,255,0,100},  {255,255,0,100},  {255,255,0,100},  {255,255,0,100},  {255,255,0,100},
    //绿
    {0,255,0,100},    {0,255,0,100},    {0,255,0,100},    {0,255,0,100},    {0,255,0,100},    {0,255,0,100},
    //蓝
    {0,0,255,100},    {0,0,255,100},    {0,0,255,100},    {0,0,255,100},    {0,0,255,100},    {0,0,255,100},
    //靛
    {0,255,255,100},  {0,255,255,100},  {0,255,255,100},  {0,255,255,100},  {0,255,255,100},  {0,255,255,100},
    //紫
    {255,0,255,100},  {255,0,255,100},  {255,0,255,100},  {255,0,255,100},  {255,0,255,100},  {255,0,255,100},
};

//红灯警报
RGB_t rgbTable_Alarm[LED_MAX_NUM*TABLE_SIZE_Alarm] =      //500ms  0.3
{
    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},    {255,0,0,100},
    {0,0,0,0},        {0,0,0,0},        {0,0,0,0},        {0,0,0,0},        {0,0,0,0},        {0,0,0,0},
};

//黄色常亮
RGB_t rgbTable_Yellow[LED_MAX_NUM*TABLE_SIZE_Yellow] = 
{
    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},    {255,255,0,100},
};

uint8_t tableIndex = 0;
RGB_t * rgbTablePt = rgbTable_Alarm;
uint16_t FadingTime = SECTION_TIME_1*0.3;
uint8_t TableMaxSize = TABLE_SIZE_Alarm;
uint8_t ModeSelection = 0;
bool Flip_flag = true;
void App1TimerExpired(SoftTimer_t *timer)      
{
    CLM_SetRGBL(ROOM_TEMPERATURE, LED0,rgbTablePt[tableIndex*LED_MAX_NUM +0].green, rgbTablePt[tableIndex*LED_MAX_NUM +0].blue, rgbTablePt[tableIndex*LED_MAX_NUM +0].red, rgbTablePt[tableIndex*LED_MAX_NUM +0].level, FadingTime);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED1,rgbTablePt[tableIndex*LED_MAX_NUM +1].green, rgbTablePt[tableIndex*LED_MAX_NUM +1].blue, rgbTablePt[tableIndex*LED_MAX_NUM +1].red, rgbTablePt[tableIndex*LED_MAX_NUM +1].level, FadingTime);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED2,rgbTablePt[tableIndex*LED_MAX_NUM +2].green, rgbTablePt[tableIndex*LED_MAX_NUM +2].blue, rgbTablePt[tableIndex*LED_MAX_NUM +2].red, rgbTablePt[tableIndex*LED_MAX_NUM +2].level, FadingTime);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED3,rgbTablePt[tableIndex*LED_MAX_NUM +3].green, rgbTablePt[tableIndex*LED_MAX_NUM +3].blue, rgbTablePt[tableIndex*LED_MAX_NUM +3].red, rgbTablePt[tableIndex*LED_MAX_NUM +3].level, FadingTime);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED4,rgbTablePt[tableIndex*LED_MAX_NUM +4].green, rgbTablePt[tableIndex*LED_MAX_NUM +4].blue, rgbTablePt[tableIndex*LED_MAX_NUM +4].red, rgbTablePt[tableIndex*LED_MAX_NUM +4].level, FadingTime);
    CLM_SetRGBL(ROOM_TEMPERATURE, LED5,rgbTablePt[tableIndex*LED_MAX_NUM +5].green, rgbTablePt[tableIndex*LED_MAX_NUM +5].blue, rgbTablePt[tableIndex*LED_MAX_NUM +5].red, rgbTablePt[tableIndex*LED_MAX_NUM +5].level, FadingTime);  
    
    switch (ModeSelection)
    {
        case 0x10:      //单色
          SoftTimer_Stop(&App1Timer);
        break;
        
        case 0x20:      //单色呼吸
          if(Flip_flag) {tableIndex++;Flip_flag = false;}
          else {tableIndex--;Flip_flag = true;SoftTimer_Stop(&App1Timer);}
        break;
        
        case 0x30:      //多色呼吸
          if(Flip_flag) {tableIndex++;Flip_flag = false;}
          else {Flip_flag = true;SoftTimer_Stop(&App1Timer);}
          if(tableIndex>=TableMaxSize) tableIndex = 0;  //循环
        break;
        
        case 0x40:      //无极变色
          SoftTimer_Stop(&App1Timer);
        break;
        
        case 0x50:      //红灯警报
          if(Flip_flag) {tableIndex++;Flip_flag = false;}
          else {tableIndex--;Flip_flag = true;SoftTimer_Stop(&App1Timer);}
        break;
        
        case 0x60:      //黄灯常亮
          SoftTimer_Stop(&App1Timer);
        break;
        
        case 0x70:      //多彩1
          tableIndex++;
          if((tableIndex == TABLE_SIZE_Flow_1/2)||(tableIndex == TABLE_SIZE_Flow_1)) SoftTimer_Stop(&App1Timer);
        break;
        
        case 0x80:      //多彩2
          tableIndex++;
          if((tableIndex == TABLE_SIZE_Flow_2/2)||(tableIndex == TABLE_SIZE_Flow_2)) SoftTimer_Stop(&App1Timer);
        break;
        
    default:        //上电默认运行
          tableIndex++;
          if(tableIndex>=TableMaxSize) tableIndex = 0;
        break;
    }
}

//uint16_t green,blue,red = 0;
//bool flag_green = true,flag_blue = true,flag_red = true;
//bool flag_judgeBlue = false,flag_judgeGreen = false;
//uint32_t FADING_TIME_2 = SECTION_TIME_2*0.7;
//void App2TimerExpired(SoftTimer_t *timer)       
//{
//    CLM_SetRGBL(ROOM_TEMPERATURE,LED0,green,blue,red,100,FADING_TIME_2);
//    CLM_SetRGBL(ROOM_TEMPERATURE,LED1,green,blue,red,100,FADING_TIME_2);
//    CLM_SetRGBL(ROOM_TEMPERATURE,LED2,green,blue,red,100,FADING_TIME_2);
//    CLM_SetRGBL(ROOM_TEMPERATURE,LED3,green,blue,red,100,FADING_TIME_2);
//    CLM_SetRGBL(ROOM_TEMPERATURE,LED4,green,blue,red,100,FADING_TIME_2);
//    CLM_SetRGBL(ROOM_TEMPERATURE,LED5,green,blue,red,100,FADING_TIME_2);
//
//    if(flag_red == true) red+=10;
//    else red-=10;
//    if(red>249 || red<10){
//      flag_red = !flag_red;
//      if(flag_blue == true) blue+=10;
//      else blue-=10;
//      flag_judgeBlue = true;
//    }
//    if((flag_judgeBlue)&&(blue>249 || blue<10)){
//      flag_blue = !flag_blue;
//      if(flag_green == true) green+=10;
//      else green-=10;
//      flag_judgeBlue = false;
//      flag_judgeGreen = true;
//    }
//    if((flag_judgeGreen)&&(green>249 || green<10)){
//      flag_green= !flag_green;
//      flag_judgeGreen = false;
//    }
//}

void APPL_TaskHandler(void)
{
    switch(applState)
    {
      case TASK_STATE_INIT:       //执行一次
        applState = TASK_STATE_ACTIVE;
        SoftTimer_Start(&App1Timer);
//        SoftTimer_Start(&App2Timer);
//        SoftTimer_Start(&colorCompensationTimer);
                
//        UART_Init(BAUDRATE_9600, BITSIZE_8BITS, PARITY_NONE,STOPS_1BITS);
//        UART_RegisterIRQ(Uart_RX_ISR, NULL);
//        
//        三个GPIO口，没接出来
//        GPIO_Init(GPIO_PORT_1, GPIO_MUX_GPIO, GPIO_DIR_OUTPUT,GPIO_PULL_MODE_NONE);
//        GPIO_Init(GPIO_PORT_2, GPIO_MUX_GPIO, GPIO_DIR_OUTPUT,GPIO_PULL_MODE_NONE);
//        GPIO_Init(GPIO_PORT_3, GPIO_MUX_GPIO, GPIO_DIR_OUTPUT,GPIO_PULL_MODE_NONE);
//              
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), 0);
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_1));
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_2));
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3));
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_2) | (1 << GPIO_PORT_1));
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3) | (1 << GPIO_PORT_1));
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3) | (1 << GPIO_PORT_2));
//        GPIO_SetPorts((1 << GPIO_PORT_1) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_3), (1 << GPIO_PORT_3) | (1 << GPIO_PORT_2) | (1 << GPIO_PORT_1));
        break;
        
      case TASK_STATE_ACTIVE:
        break;
    }
}


//void colorCompensationTimerExpired(SoftTimer_t *timer)
//{
//    uint8_t ledNo = 0;
//    uint16_t pwm[3];
//    CLM_GetCurrentPWMs(&pwm[LED_RED], &pwm[LED_GREEN], &pwm[LED_BLUE]);
////    DEBUG_OUT("PWM R: %d, PWM_G: %d, PWM_B: %d \r\n",pwm[CHANNEL_RED],pwm[CHANNEL_GREEN],pwm[CHANNEL_BLUE]);
//    if (CLM_FadingIsFinished() == true)
//    {
//        ColorParam_t *color = CLM_GetTargetColor();
//        int16_t temperature = ADC_GetLedTemperature();
////        DEBUG_OUT("T: %d\r\n",temperature);
//        switch(color->colorMode)
//        {
//          case COLOR_MODE_RGB:
//            CLM_SetRGB(temperature, ledNo, color->RGB.red, color->RGB.green, color->RGB.blue,1000);
//            break;
//          case COLOR_MODE_RGBL:
//            CLM_SetRGBL(temperature, ledNo, color->RGBL.red, color->RGBL.green, color->RGBL.blue,color->RGBL.level,1000);
//            break;
//          case COLOR_MODE_HSL:
//            CLM_SetHSL(temperature, ledNo, color->HSL.hue, color->HSL.saturation, color->HSL.level, 1000);
//            break;
//          case COLOR_MODE_XYY:
//            CLM_SetXYY(temperature, ledNo, color->XYY.x, color->XYY.y, color->XYY.Y, 1000);
//            break;
//        }
//    }
//}


